<?php 
$auth='username:password';
$dbhost='localhost';
$dbuser='dbuser';
$dbpwd='dbpwd';
$database='database';

$json_source='http://twitter.com/statuses/friends_timeline.json';

$dbtable='REC';

$ch=curl_init($json_source);
$headers  =  array( "User-Agent: Math2Gold Examples" );
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_USERPWD,$auth);
curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:9050');
curl_setopt($ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
$result=curl_exec($ch);
curl_close($ch);

$obj = json_decode($result);


$link = mysql_connect($dbhost,$dbuser,$dbpwd)
    or die('Could not connect: ' . mysql_error());
mysql_select_db($database) or die('Could not select database');

$stack=array();
foreach($obj as $d) 
{

#print '@'.$d->{'user'}->{"screen_name"}."\t".$d->{'text'}."\t(".$d->{'created_at'}.")".PHP_EOL;
$query = 'INSERT INTO '.$dbtable.'(username,content,hostdate,rectime) VALUES(\''.mysql_real_escape_string($d->{'user'}->{"screen_name"}).'\','.
'\''.mysql_real_escape_string($d->{'text'}).'\','.
'\''.mysql_real_escape_string($d->{'created_at'}).'\',CURRENT_TIMESTAMP())';
array_push($stack, $query);
}

$newarrive=0;
while (count($stack)>0)
{
	$result = mysql_query(array_pop($stack)) or $newarrive--;
	$newarrive++;
}

print "New:".$newarrive.PHP_EOL;
mysql_close($link);

?>
